<?php 
  //test query http://localhost/onmotion/listPoints.php?userName=test&userTid=222222
  
  include_once('session.php');
  include_once('db.php');
  //header("Content-Type: application/xml; charset=utf-8");
  header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
  header('Content-Disposition: attachment; filename="onmotion.kml"');
  
  if ( $_SERVER['REQUEST_METHOD'] === 'POST' )
  {    
    $selectedUser = $_POST['user'];        
  } else {    
    $selectedUser = $_GET['user'];    
  }
       
  $doc = new DomDocument('1.0', 'UTF-8');
  $doc->formatOutput = true;
  $kmlNs = $doc->createElementNS('http://earth.google.com/kml/2.1','kml');
  $doc->appendChild($kmlNs);
   
  $document = $doc->createElement('Document');
  
  $name = $doc->createElement('name');
  $nameValue = $doc->createTextNode($product_name.' Document ');
  $name->appendChild($nameValue);
    
  $description = $doc->createElement('description');
  $descriptionValue = $doc->createTextNode($product_name.' - Invalid Security');
  $description->appendChild($descriptionValue);
  
  $document->appendChild($name);
  $document->appendChild($description);
  
  $schema = $doc->createElement('Schema');
  $schema->setAttribute("parent", "Placemark");
  $schema->setAttribute("name", "FindMePlacemark");
  
  $fieldDate = $doc->createElement('SimpleField');
  $fieldDate->setAttribute("type","string");
  $fieldDate->setAttribute("name","date");
  $schema->appendChild($fieldDate);
    
  $fieldImage = $doc->createElement('SimpleField');
  $fieldImage->setAttribute("type","string");
  $fieldImage->setAttribute("name","image");
  $schema->appendChild($fieldImage);
  
  $document->appendChild($schema);
          
  if ( IsSessionValid())
  {        
    $conn = &ADONewConnection($dbType);
    $conn->PConnect($dbServer, $dbUser, $dbPassword, $dbName);       
            
    if (!$selectedUser){
      $selectedUser = $sessionUserId;
      $title = "Your Posts";
    } else {
      $query = "select name from user where id_user=$selectedUser";
      $rs = $conn->Execute($query);
      foreach ($rs as $row) {
        $title = $row[0]." Posts";  
      }
    }
            
    $query = "select lat, lng, altitude, name, comment, time, id_point, type, track from point where type <= 2 and id_user=$selectedUser order by time desc";
    //execute query
    //$conn->debug = true;      
    $rs = $conn->Execute($query);
    
    if ( $rs->RecordCount() > 0) {
      $description->replaceChild($doc->createTextNode($title), $descriptionValue);
    } else {
      $description->replaceChild($doc->createTextNode('No Posts Available!'), $descriptionValue);
    } 
    
              
    // create Point Placemark
    foreach ($rs as $row) 
    {
      $placeMark = $doc->createElement('FindMePlacemark');
      $placeMark->setAttribute("id",$row[6]);
      $name = $doc->createElement('name');
      $nameValue = $doc->createTextNode($row[3]);
      $name->appendChild($nameValue);
      $placeMark->appendChild($name);
      
      $description = $doc->createElement('description');
      $descriptionValue = $doc->createTextNode($row[4]);
      $description->appendChild($descriptionValue);
      $placeMark->appendChild($description);
      
      $date = $doc->createElement('date');
      $dateValue = $doc->createTextNode($row[5]);
      $date->appendChild($dateValue);
      $placeMark->appendChild($date);
      
      $user = $doc->createElement('user');
      $userValue = $doc->createTextNode($sessionUserName);
      $user->appendChild($userValue);
      $placeMark->appendChild($user);
      
      // select resources
      $image = $doc->createElement('image');
      $imageValue = $doc->createTextNode("0");
      $image->appendChild($imageValue);
      $queryResource = "select id_image from image where id_point=$row[6]";
      $rsResource = $conn->Execute($queryResource);
      foreach($rsResource as $rowResource) {
          $image->replaceChild($doc->createTextNode($rowResource[0]),$imageValue);
      }
      $placeMark->appendChild($image);

      // check point type
      if ($row[7] == 0 || $row[7] == 1) {
        $point = $doc->createElement('Point');
        $coordinates = $doc->createElement('coordinates');
        $pointValue = $doc->createTextNode($row[1].','.$row[0].','.$row[2]);
        $coordinates->appendChild($pointValue);
        $point->appendChild($coordinates);
        $placeMark->appendChild($point);
      } else if ($row[7] == 2) { 
        $line = $doc->createElement('LineString');      
        $coordinates = $doc->createElement('coordinates');
        
        $extrude = $doc->createElement('extrude');  
        $extrudeValue = $doc->createTextNode('1');
        $extrude->appendChild($extrudeValue);
        $line->appendChild($extrude);
        
        $tessellate = $doc->createElement('tessellate');  
        $tessellateValue = $doc->createTextNode('1');
        $tessellate->appendChild($tessellateValue);
        
        $line->appendChild($tessellate);
        
        $lineContent = '';        
        $queryTrack = "select lat, lng, altitude from point where track = $row[8] and id_user=$selectedUser order by time ASC";              
        $rsTrack = $conn->Execute($queryTrack);
        foreach ($rsTrack as $rowTrack) {
          $lineContent .= $rowTrack[1].','.$rowTrack[0].','.$rowTrack[2].' ';
        }
        $coordinatesValue = $doc->createTextNode($lineContent);
        $coordinates->appendChild($coordinatesValue);
        $line->appendChild($coordinates);
        $placeMark->appendChild($line);
      }                                             
      
      $document->appendChild($placeMark);                 
    }
    
    $rs->close();
    $conn->close();   
  }      
   
  $kmlNs->appendChild($document);        
     
  // save to xml file
  $xml_string = $doc->saveXML();
  // output in UTF-8
  echo $xml_string;
  // echo iconv("ISO-8859-1", "UTF-8",$xml_string);
?>
